﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DMS.Data;
using DMS.Business.BaseClass;

namespace DMS.Business.Modules.SystemManagement
{
    /// <summary>
    /// 枚举服务
    /// </summary>
    public class EnumService : BaseService
    {
        public List<SYS_EnumTypes> GetEnumTypes()
        {
            return DB.SYS_EnumTypes.ToList();
        }

        public List<SYS_Enums> GetEnums(string enumTypeID)
        {
            return DB.SYS_Enums
                    .Where(p => p.EnumTypeID.Contains(enumTypeID)
                    && !(bool)p.IsDeleted
                    )
                    .OrderBy(p => p.Sequence)
                    .ToList();
        }

        public List<SYS_Enums> GetEnums(string enumTypeID, int pageIndex, int pageSize, out int total)
        {
            total = DB.SYS_Enums
                .Where(p => p.EnumTypeID.Contains(enumTypeID) && !(bool)p.IsDeleted).Count();

            int skinCount = (pageIndex - 1) * pageSize;
            List<SYS_Enums> list = DB.SYS_Enums
                .Where(p => p.EnumTypeID.Contains(enumTypeID) && !(bool)p.IsDeleted)
                .OrderBy(p => p.EnumTypeID)
                .Skip(skinCount)
                .Take(pageSize)
                .ToList();
            return list;
        }

        public SYS_Enums GetByID(string id)
        {
            return DB.SYS_Enums
                .First(p => p.EnumID.Equals(id));
        }

        /// <summary>
        /// 通过枚举类的类型ID及编码数组获取枚举类
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="codes"></param>
        /// <returns></returns>
        public List<SYS_Enums> GetByEnumCodes(string typeId , string[] codes)
        {
            var enums = from e in DB.SYS_Enums
                         where e.EnumTypeID == typeId
                               && (codes).Contains(e.EnumCode)
                         orderby e.Sequence
                         select e;
            return enums.ToList();
        }

        public void Add(SYS_Enums obj)
        {
            obj.IsDeleted = false;
            DB.AddToSYS_Enums(obj);
            DB.SaveChanges();
        }

        public void Update()
        {
            DB.SaveChanges();
        }

        public void Delete(string id)
        {
            SYS_Enums obj = GetByID(id);
            if (obj != null)
            {
                obj.IsDeleted = true;
                DB.SaveChanges();
            }
        }
    }
}
